import { List } from 'list-model.js';

var list = new List();
var app = getApp()
Page({
  data: {
    startX: 0,
    itemLefts: [0],
    addr:[]
  },
  onLoad: function (options) {
    //this._loadData();
  },
  _loadData: function (callback) {
    var that = this;
    //获得product信息
    list.getData((data) => {

      that.setData({
        addr: data,
      });

    });

  },
	add: function () {
		wx.navigateTo({
			url: '../add/add'
		});
	},
  edit: function (e) {
    var that = this;
    // 取得下标
    var index = parseInt(e.currentTarget.dataset.index);
    // 取出id值
    var objectId = this.data.addr[index]['uaddres_id'];
    wx.navigateTo({
      url: '../add/add?objectId=' + objectId
    });
  },
  onShow:function(){
    this._loadData();
  },
  delete: function (e) {
    var that = this;
    // 取得下标
    var index = parseInt(e.currentTarget.dataset.index);
    // 找到当前地址AVObject对象
    var address = that.data.addr[index]['uaddres_id'];
    // 给出确认提示框
    wx.showModal({
      title: '确认',
      content: '要删除这个地址吗？',
      success: function (res) {
        if (res.confirm) {
          // 真正删除对象
          list.destroyAddr(address, (data) => {
            // 删除成功提示
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            });
            // 重新加载数据
            that._loadData();
          }, function (error) {

          });
        }
      }
    })
    var itemLefts = this.data.itemLefts;
    //console.log("itemLefts" + itemLefts);
    itemLefts[index] = 0;
    this.setData({
      itemLefts: itemLefts
    });
  },
  touchStart: function (e) {
    var startX = e.touches[0].clientX;
    console.log("startX" + startX);
    this.setData({
      startX: startX,
      itemLefts: []
    });
  },
  touchMove: function (e) {
    var index = e.currentTarget.dataset.index;
    //var index = 0;
    var movedX = e.touches[0].clientX;
    var distance = this.data.startX - movedX;
    var itemLefts = this.data.itemLefts;
    console.log("index" + index);
    console.log("movedX" + movedX);
    console.log("distance" + distance);
    console.log("itemLefts" + itemLefts);
    itemLefts[index] = -distance;
    this.setData({
      itemLefts: itemLefts
    });
  },
  touchEnd: function (e) {
    var index = e.currentTarget.dataset.index;
    //var index = 0;
    var endX = e.changedTouches[0].clientX;
    var distance = this.data.startX - endX;
    console.log("index" + index);
    console.log("endX" + endX);
    console.log("distance" + distance);
    // button width is 60
    var buttonWidth = 140;
    if (distance <= 0) {
      distance = 0;
    } else {
      if (distance >= buttonWidth) {
        distance = buttonWidth;
      } else if (distance >= buttonWidth / 2) {
        distance = buttonWidth;
      } else {
        distance = 0;
      }
    }
    var itemLefts = this.data.itemLefts;
    console.log("itemLefts" + itemLefts);
    itemLefts[index] = -distance;
    this.setData({
      itemLefts: itemLefts
    });
  } ,
  goBack:function(e){
    var pages = getCurrentPages(); //获取加载的页面
    var currentPage = pages[pages.length - 2]; //获取当前页面的对象
    var index = e.currentTarget.dataset.index;
    console.log(currentPage)
    if (currentPage.route =="pages/order/checkout/checkout"){
      currentPage.setData({
        addressList: this.data.addr[index],
      });
      wx.navigateBack({
        changed: true
      })
     }    
  }
})